<template>
	<view>
		<view class="search-box">
			<uni-search-bar  @input="input" :radius="100"  cancelButton="none" ></uni-search-bar>
		</view>
		
		<!-- 搜索建议列表 -->
		<view class="sug-list" v-if="searchResults.length !== 0">
			<view class="sug-item" v-for="(item,i) in searchResults"  :key="i" @click="gotoDetail(item)">
				<view class="goods-name">
					{{item.goods_name}}
				</view>
				<uni-icons type="arrowright"  size="16" ></uni-icons>
			</view>
		</view>
		
		<!-- 搜索历史列表-->
		<view class="history-box" v-else>
			<!-- 标题区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash"  size="17" @click="clean"></uni-icons>
			</view>
			<!-- 列表区域 -->
			<view class="history-list">
				<uni-tag :text="item2" 
				type="default" :circle="true" 
				v-for="(item2,i2) in hitstories" :key="i2"
				@click="gotoGoodsList(item2)"
				></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//延时器的timerid
				timer:null,
				//搜索关键词
				kw:'',
				//搜索关键词的数组数据
				searchResults:[],
				//搜索历史的数组信息
historyList:[]
			};
		},
		onLoad() {
			// 页面加载调用持久化数据
	this.historyList = JSON.parse(uni.getStorageSync('kw'))	
		},
		methods:{
			//inout输入事件的函数
			input(e){
				clearTimeout(this.timer)
				// console.log(e)
			this.timer = setTimeout(() => {
					// console.log(e)
					this.kw = e
					// console.log(this.kw)
					this.getSearchList()
				},600)
			},
		async getSearchList(){
				//判断关键词是否为空
				if(this.kw.length === 0){
					this.searchResults = []
					return
				}
				// 如果不为空就发起请求
		const { data : res} = await uni.$http.get('/api/public/v1/goods/qsearch',{ query : this.kw})
		if(res.meta.status !== 200 ) return uni.$showMsg()
		this.searchResults = res.message
		this.saveSearchHistory()
			},
			// 点击商品跳转详情页面
			gotoDetail(item){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			// 将关键词存到相关数组数据里
			saveSearchHistory(){
		 // this.historyList.push(this.kw)
		 const set = new Set(this.historyList)
		 //先转化未set对象  关键词重复的 先删除 再添加就可以实现关键词在最新的位置
		 set.delete(this.kw)
		 set.add(this.kw)
		 //把set对象转化为真的数组
		 this.historyList =Array.from(set)
		 // 对搜索历史数据进行持久化的操作
		 uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			// 清空存储的历史记录
			clean(){
				this.historyList = []
				uni.setStorageSync('kw','[]')
			},
			// 点击历史记录到商品列表页面
			gotoGoodsList(kw){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?query=' + kw
				})
			}
			
		},
		computed:{
			hitstories(){
			return	[...this.historyList].reverse()
			},
		
		}
	}
</script>

<style lang="scss">
.search-box{
	position: sticky;
	top: 0;
	z-index: 999;
	background-color: #26a886;
	
}
// 搜索出的内容的样式
.sug-list{
	padding: 0 12rpx;
	.sug-item{
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 24rpx;
		padding: 28rpx 0;
		border-bottom: 1px solid #1da87e;
		.goods-name{
			//多的内容不换行
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
			
		}
	}
}


// 搜索历史样式
.history-box {
	padding: 0 14rpx;
	.history-title{
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 80rpx;
		font-size: 24rpx;
		border-bottom: 1px solid #1da87e;
	}
	.history-list{
		display: flex;
		flex-wrap: wrap;
		.uni-tag{
			margin-right: 10rpx;
		}
	}
}
</style>
